package com.soar.tbox.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;

import com.soar.tbox.protocol.DispatchProtocol;

public class SmsReceiver extends BroadcastReceiver {

	public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";

	@Override
	public void onReceive(Context context, Intent intent) {

		if (intent.getAction().equals(SMS_RECEIVED_ACTION)) {
			SmsMessage[] messages = getMessagesFromIntent(intent);
			for (SmsMessage message : messages) {
				// System.out.println(message.getOriginatingAddress() + " : " +
				// message.getDisplayOriginatingAddress() + " : " +
				// message.getDisplayMessageBody() + " : " +
				// message.getTimestampMillis());
				// 显示接收的内容
				final String sms = message.getDisplayMessageBody();
				Thread thread = new Thread(){
					public void run(){
						DispatchProtocol.doDispatch(sms);
					}
				};
				thread.start();
			}
		}
	}

	/**
	 * 从intent中或曲解短信信息
	 */
	public final SmsMessage[] getMessagesFromIntent(Intent intent) {
		Object[] messages = (Object[]) intent.getSerializableExtra("pdus");
		byte[][] pduObjs = new byte[messages.length][];

		for (int i = 0; i < messages.length; i++) {
			pduObjs[i] = (byte[]) messages[i];
		}
		byte[][] pdus = new byte[pduObjs.length][];
		int pduCount = pdus.length;
		SmsMessage[] msgs = new SmsMessage[pduCount];
		for (int i = 0; i < pduCount; i++) {
			pdus[i] = pduObjs[i];
			msgs[i] = SmsMessage.createFromPdu(pdus[i]);
		}
		return msgs;
	}

}